package com.one.shopping.api;

import android.content.Context;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.one.shopping.entity.Goods;
import com.one.shopping.entity.Member;
import com.one.shopping.entity.Orders;
import com.one.shopping.entity.Users;
import com.one.shopping.util.PostUtil;

import java.util.HashMap;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/9 10:20
 */
public class OneApi {

//    public final static String HOST = "121.40.56.230";
    public final static String HOST = "192.168.0.249";
    public final static String HTTP = "http://";
//    public final static String POST = ":8580";
    public final static String POST = ":8080";
    public final static String BASE_URL = HTTP + HOST + POST + "/oneServer";
    public final static int PAGE_SIZE = 10;
    public final static String IMAGE_URL = BASE_URL + "/image/";

    /**
     * 登录接口
     */
    public static void login(Users users, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("name", users.getName());
        params.put("passwd", users.getPasswd());
        AsyncHttpHelp.post(BASE_URL + "/api/login", params, handler);
    }

    /**
     * 注册用户接口
     */
    public static void registerUser(Users user, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("name", user.getName());
        params.put("passwd", user.getPasswd());
        AsyncHttpHelp.post(BASE_URL + "/api/addUser", params, handler);
    }

    /**
     * 更新用户接口
     */
    public static void updateUser(Users user, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", user.getId() + "");
        params.put("name", user.getName());
        params.put("passwd", user.getPasswd());
        AsyncHttpHelp.post(BASE_URL + "/api/updateUser", params, handler);
    }

    /**
     * 删除用户接口
     */
    public static void deleteUser(int id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", id + "");
        AsyncHttpHelp.post(BASE_URL + "/api/deleteUser", params, handler);
    }

    /**
     * 获取用户列表
     */
    public static void getUserList(AsyncHttpResponseHandler handler) {
        AsyncHttpHelp.get(BASE_URL + "/api/queryUser", handler);
    }


    /**
     * 注册会员接口
     */
    public static void registerMember(Member member, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("userName", member.getUserName());
        params.put("phone", member.getPhone());
        params.put("score", member.getScore());
        params.put("money", member.getMoney());
        AsyncHttpHelp.post(BASE_URL + "/api/addMember", params, handler);
    }

    /**
     * 更新会员接口
     */
    public static void updateMember(Member member, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("cardId", member.getCardId());
        params.put("userName", member.getUserName());
        params.put("phone", member.getPhone());
        params.put("score", member.getScore());
        params.put("money", member.getMoney());
        AsyncHttpHelp.post(BASE_URL + "/api/updateMember", params, handler);
    }

    /**
     * 删除会员接口
     */
    public static void deleteMember(int cardId, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("cardId", cardId + "");
        AsyncHttpHelp.post(BASE_URL + "/api/deleteMember", params, handler);
    }

    /**
     * 获取会员列表
     */
    public static void getMemberList(AsyncHttpResponseHandler handler) {
        AsyncHttpHelp.get(BASE_URL + "/api/queryMember", handler);
    }


    /**
     * 添加商品接口
     */
    public static void addGoods(Goods goods, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", goods.getId());
        params.put("name", goods.getName());
        params.put("image", goods.getImage());
        params.put("stock", goods.getStock());
        params.put("price", goods.getPrice());
        AsyncHttpHelp.post(BASE_URL + "/api/addGoods", params, handler);
    }

    /**
     * 修改商品接口
     */
    public static void updateGoods(Goods goods, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", goods.getId());
        params.put("name", goods.getName());
        params.put("image", goods.getImage());
        params.put("stock", goods.getStock());
        params.put("price", goods.getPrice());
        AsyncHttpHelp.post(BASE_URL + "/api/updateGoods", params, handler);
    }

    /**
     * 删除会员接口
     */
    public static void deleteGoods(String id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", id + "");
        AsyncHttpHelp.post(BASE_URL + "/api/deleteGoods", params, handler);
    }

    /**
     * 获取商品列表
     */
    public static void getGoodsList(AsyncHttpResponseHandler handler) {
        AsyncHttpHelp.get(BASE_URL + "/api/queryGoods", handler);
    }

    /**
     * 上传图片
     */
    public static void uploadImage(final Context context, final String filePath) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("userId", "0123456789");
                PostUtil.postImg(context, BASE_URL + "/api/uploadImage", filePath, params);
            }
        }.start();
    }

    /**
     * 模糊查询商品
     */
    public static void getGoodListByCondition(String condition,AsyncHttpResponseHandler handler){
        RequestParams params = new RequestParams();
        params.put("condition", condition);
        AsyncHttpHelp.get(BASE_URL + "/api/queryGoodsByCondition", params, handler);
    }

    /**
     * 查询会员卡号是否存在
     */
    public static void queryCardId(int cardId, AsyncHttpResponseHandler handler){
        RequestParams params = new RequestParams();
        params.put("cardId", cardId);
        AsyncHttpHelp.get(BASE_URL + "/api/queryMemberById", params, handler);
    }

    /**
     * 添加订单
     */
    public static void addOrders(Orders orders, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        if (orders.getCardId() != 0) {
            params.put("cardId", orders.getCardId() + "");
        }
        params.put("id", orders.getId() + "");
        params.put("total", orders.getTotal() + "");
        params.put("goodLuck", orders.getGoodLuck() + "");
        params.put("userId", orders.getUserId() + "");
        params.put("time", orders.getDate());
        AsyncHttpHelp.post(BASE_URL + "/api/addOrders", params, handler);
    }

    /**
     * 删除订单
     */
    public static void deleteOrders(String orderId, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("orderId", orderId);
        AsyncHttpHelp.post(BASE_URL + "/api/deleteOrders", params, handler);
    }

    /**
     * 添加订单详情
     */
    public static void addOrdersDetail(Orders order, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("id", order.getId() + "");
        params.put("goodsId", order.getGoodsId() + "");
        params.put("quantity", order.getQuantity() + "");
        params.put("total", order.getTotal() + "");
        params.put("date", order.getDate() + "");
        AsyncHttpHelp.post(BASE_URL + "/api/addOrdersDetail", params, handler);
    }

    /**
     * 更新商品库存量
     */
    public static void updateGoodsStock(String goodsId,int quantity, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("goodsId", goodsId);
        params.put("quantity", quantity);
        AsyncHttpHelp.get(BASE_URL + "/api/updateGoodsStock", params, handler);
    }

    /**
     * 更新会员积分
     */
    public static void updateMemberScore(int cardId,double memberScore, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("cardId", cardId + "");
        params.put("score", memberScore+"");
        AsyncHttpHelp.get(BASE_URL + "/api/updateMemberScore", params, handler);
    }

    /**
     * 查询订单
     */
    public static void queryOrders(AsyncHttpResponseHandler handler) {
        AsyncHttpHelp.get(BASE_URL + "/api/queryOrders", handler);
    }

}
